<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="请输入保修单单号"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getListData">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="90">
            </el-table-column>
            <el-table-column type="index" label="序号" width="190">
            </el-table-column>
            <el-table-column prop="sn" label="保修单单号" width="200" sortable>
            </el-table-column>
            <el-table-column prop="guaranteeTime" label="保修时间" width="200" sortable>
            </el-table-column>
            <el-table-column prop="guaranteeContent" label="保修内容" width="200" sortable>
                <template scope="scope">
                    <el-button size="small" type="button" @click="openGuaranteeContent(scope.$index, scope.row)">点击查看</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="solve" label="是否解决"  :formatter="formatSolve" width="180" sortable>
            </el-table-column>

            <el-table-column label="操作" fixed="right" width="230">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <!--<el-button type="success" @click="creatContract" :disabled="this.sels.length===0">生成合同</el-button>-->
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">

                <el-form-item label="保修时间" prop="guaranteeTime">
                    <div class="block">
                        <el-date-picker
                                v-model="editForm.guaranteeTime"
                                align="right"
                                type="date"
                                placeholder="选择日期"
                                :picker-options="pickerOptions">
                        </el-date-picker>
                    </div>

                </el-form-item>

                <el-form-item label="保修内容" prop="guaranteeContent">
                    <el-input v-model="editForm.guaranteeContent" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="是否解决" prop="solve">
                    <el-radio v-model="editForm.solve" label="1">已解决</el-radio>
                    <el-radio v-model="editForm.solve" label="2">未解决</el-radio>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">

                <el-form-item label="保修时间" prop="guaranteeTime">
                    <div class="block" >
                        <el-date-picker
                                v-model="addForm.guaranteeTime"
                                align="right"
                                type="date"
                                placeholder="选择日期"
                                :picker-options="pickerOptions">
                        </el-date-picker>
                    </div>
                </el-form-item>

                <el-form-item label="保修内容" prop="guaranteeContent">
                    <el-input v-model="addForm.guaranteeContent" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="是否解决" prop="solve">
                    <el-radio v-model="addForm.solve" label="1">已解决</el-radio>
                    <el-radio v-model="addForm.solve" label="2">未解决</el-radio>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import util from '../../../common/js/util'
    //import NProgress from 'nprogress'
    import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../../api/api';

    export default {
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                value1: '',
                value2: '',



                filters: {
                    name: ''
                },
                listData: [],
                total: 0,
                keyword: '',
                pageSize: 10,
                currentPage: 1,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {


                    guaranteeContent: [
                        {required: true, message: '请输入保修内容', trigger: 'blur'}
                    ],
                    solve: [
                        {required: true, message: '请输入是否解决', trigger: 'blur'}
                    ]

                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    guaranteeTime:'',
                    employee_id:'',
                    guaranteeContent:'',
                    solve:'',

                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {


                    guaranteeContent: [
                        {required: true, message: '请输入保修内容', trigger: 'blur'}
                    ],
                    solve: [
                        {required: true, message: '请输入是否解决', trigger: 'blur'}
                    ]

                },
                //新增界面数据
                addForm: {
                    guaranteeTime:'',
                    name: ''

                }/*,
                //
                contractForm:{
                    sn: '',
                    customer:'',
                    employee_id:'',
                    sum:'',
                    signTime:'',
                    intro:'',
                    tenant_id:'',
                }*/


            }
        },




        methods: {
            //保修内容
            openGuaranteeContent(index, row) {
                this.$alert(row.guaranteeContent, '保修内容', {
                    confirmButtonText: '确定',
                })
            },
            //是否解决显示转换
            formatSolve: function (row, column) {
                return row.solve == 1 ? '已解决' : row.solve == 2 ? '未解决' : '未知';
            },

            //查看摘要
            openIntro(index, row) {
                this.$alert(row.intro, '订单摘要', {
                    confirmButtonText: '确定',
                })
            },

            handleCurrentChange(val) {
                this.currentPage = val;
                this.getListData();
            },
            //获取用户列表
            getListData() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.name
                };
                this.listLoading = true;
                this.$http.post('/guaranteeDetails/findByPage',para).then(res=>{
                    let ajaxResult =  res.data
                    if (ajaxResult.success){
                        this.listData = ajaxResult.data.result;
                        this.total = ajaxResult.data.total
                    }else {
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                    this.listLoading = false;
                }).catch(error=>{
                    this.listLoading = false;
                    this.$message({
                        message: '加载失败',
                        type: 'error'
                    });
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/guaranteeDetails/delete/' + row.id, {}).then(res => {
                        let ajaxResult = res.data
                        if (ajaxResult.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            this.$http.post("/guaranteeDetails/update",para).then(res => {
                                let ajaxResult = res.data
                                if (ajaxResult.success) {
                                    this.$message({
                                        message: "修改成功",
                                        type: 'success'
                                    })
                                    this.$refs['editForm'].resetFields();
                                    this.editLoading = false;
                                    this.editFormVisible = false;
                                    this.getListData();
                                } else {
                                    this.editLoading = false;
                                    this.message({
                                        message: "修改失败",
                                        type: 'error'
                                    })

                                }
                            }).catch(error => {
                                console.debug(error)
                                this.$message({
                                    message: "修改失败",
                                    type: 'error'
                                })
                            })

                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //准备参数，将参数拷贝到para中
                            let para = Object.assign({}, this.addForm);
                            this.$http.post('/guaranteeDetails/add', para).then(res => {
                                let ajaxResult = res.data
                                if (ajaxResult.success) {
                                    this.$message.success("保存成功");
                                    this.addFormVisible = false;
                                    this.$refs['addForm'].resetFields();
                                    this.getListData();
                                } else {
                                    this.addFormVisible = false;
                                    this.$message({
                                        message: '保存失败',
                                        type: 'error'
                                    });
                                }
                                this.addLoading = false;
                            }).catch(error => {
                                this.addLoading = false;
                                this.$message({
                                    message: '保存失败',
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id)
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    this.$http.post("/guaranteeDetails/batchDelete",para).then(res=>{
                        let ajaxResult = res.data;
                        if (ajaxResult.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                })
            }
        },
        mounted() {
            this.getListData();
        }
    }

</script>

<style scoped>

</style>